package us.sunrisemorning.mykancolle.kcsapi;

import java.util.List;

import org.apache.commons.lang3.RandomUtils;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import us.sunrisemorning.mykancolle.battle.Battle;
import us.sunrisemorning.mykancolle.battle.BattleShip;
import us.sunrisemorning.mykancolle.config.GameData;
import us.sunrisemorning.mykancolle.mapdata.BaseMapData;
import us.sunrisemorning.mykancolle.mapdata.EnemyDeck;
import us.sunrisemorning.mykancolle.model.Basic;
import us.sunrisemorning.mykancolle.model.Deck;
import us.sunrisemorning.mykancolle.model.Ship;
import us.sunrisemorning.mykancolle.model.User;
import us.sunrisemorning.mykancolle.utils.ApiController;

public class ApiReqSortieController extends ApiController {
    public static final String BATTLE_DECK_ID = "battle_deck_id";
    public static final String BATTLE_MAP_DATA = "battle_map_data";
    public static final String BATTLE_INSTANCE = "battle_instance";

    public void battle() {
        User u = getCurrentUser();
        int deck_id = getSessionAttr(BATTLE_DECK_ID);
        BaseMapData mapdata = getSessionAttr(BATTLE_MAP_DATA);

        Deck deck = Deck.dao.findFirst("select * from Deck where member_id=? and id=?", u.getId(), deck_id);
        EnemyDeck edeck = mapdata.getCurrentCellEnemyInfo();
        Battle battle = new Battle(deck, edeck, u.getId(), getParaToInt("api_formation"));
        setSessionAttr(BATTLE_INSTANCE, battle);

        JSONObject result = battle.getDayBattleResult();
        result.put("dock_id", deck_id);

        renderApiJson(result);
    }

    public void battleresult() {
        User u = getCurrentUser();
        JSONObject result = new JSONObject();
        Battle battle = getSessionAttr(BATTLE_INSTANCE);
        BaseMapData mapdata = getSessionAttr(BATTLE_MAP_DATA);
        Basic basic = Basic.dao.findById(u.getId());

        result.put("ship_id", battle.getShipKe());
        result.put("win_rank", "S");
        result.put("get_exp", 0);
        result.put("mvp", 1);
        result.put("member_lv", basic.getLevel());
        result.put("member_exp", basic.getExperience());
        // TODO:经验值获得
        result.put("get_base_exp", 0);
        int[] shipExp = new int[] { -1, -1, -1, -1, -1, -1, -1 };
        List<BattleShip> fship = battle.getFship();
        for (int i = 0; i < fship.size(); i++) {
            shipExp[i + 1] = 0;
        }
        result.put("get_ship_exp", shipExp);
        JSONArray expLvup = new JSONArray();
        for (int i = 0; i < fship.size(); i++) {
            expLvup.add(new int[] { 0 });
        }
        result.put("get_exp_lvup", expLvup);
        result.put("dests", battle.getDests());
        result.put("destsf", battle.getDestsf());
        int[] lostFlag = new int[] { -1, 0, 0, 0, 0, 0, 0 };
        for (int i = 0; i < fship.size(); i++) {
            BattleShip ship = fship.get(i);
            if (ship.isDead()) {
                lostFlag[i + 1] = 1;
            }
        }
        result.put("lost_flag", lostFlag);
        result.put("quest_name", mapdata.getMapname());
        result.put("quest_level", mapdata.getMaplevel());
        result.put("enemy_info", battle.getEnemyInfo());
        // TODO:海域是否首次完成
        result.put("first_clear", 0);
        result.put("mapcell_incentive", 0);
        // TODO:掉落
        boolean get401 = RandomUtils.nextInt(0, 100) < 4 ? true : false;
        result.put("get_flag", new int[] { 0, get401 ? 1 : 0, 0 });
        if (get401) {
            Ship ship = Ship.dao.generateShip(10001, u.getId());
            ship.save();
            JSONObject shipdata = GameData.getShipData().get(10001);
            JSONObject shipinfo = new JSONObject();
            shipinfo.put("ship_id", shipdata.get("api_id"));
            shipinfo.put("ship_type", shipdata.get("api_stype"));
            shipinfo.put("ship_name", shipdata.get("api_name"));
            shipinfo.put("ship_getmes", shipdata.get("api_getmes"));
            result.put("get_ship", shipinfo);
        }
        result.put("get_eventflag", 0);
        result.put("get_exmap_rate", 0);
        result.put("get_exmap_useitem_id", 0);

        battle.saveFship();
        renderApiJson(result);
    }
}
